package cn.myallways.iglp.web.controller.mdm;


import ${EntityClass};
import com.allways.dubbo.mdm.exception.MdmException;
import ${serviceInterFace};
import cn.myallways.iglp.constants.BusiConstants;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ${ZhKey}信息Controller
 * Created by ${author} on ${createDate}.
 */
@Controller
@RequestMapping("/mdm/${key}")
public class ${UpperKey}Controller extends BaseController {

    @Autowired
    I${UpperKey}Service ${key}Service;
    /**
     * 跳转新增${ZhKey}页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/${key}/add";
    }
    /**
     * 新增${ZhKey}信息
     * @param ${key} ${ZhKey}对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void add${UpperKey}(@RequestBody ${UpperKey} ${key},HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        ${key}.setCreateUser(getSessionUser().getUserId());
        ${key}.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            ${key}Service.add${UpperKey}(${key});
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑${ZhKey}页面
     * @param ${key}Code  ${ZhKey}代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String ${key}Code,Model model){
        model.addAttribute("${key}", ${key}Service.get${UpperKey}ByCode(${key}Code));
        return "mdm/${key}/edit";
    }

    /**
     * 编辑${ZhKey}信息
     * @param ${key}
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void edit${UpperKey}(@RequestBody ${UpperKey} ${key},HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        ${UpperKey} old${UpperKey}=${key}Service.get${UpperKey}ByCode(${key}.get${UpperKey}Code());
        <#list noCommonFieldList as field>
        old${UpperKey}.set${field.upperName}(${key}.get${field.upperName}());
        </#list>
        old${UpperKey}.setUpdateUser(getSessionUser().getUserId());
        old${UpperKey}.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            ${key}Service.update${UpperKey}(old${UpperKey});
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除${ZhKey}信息
     * @param ${key} ${ZhKey}对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void delete${UpperKey}(@RequestBody ${UpperKey} ${key},HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        ${key}.setUpdateUser(getSessionUser().getUserId());
        ${key}.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            ${key}Service.delete${UpperKey}(${key});
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

	/**
     * 跳转${ZhKey}明细页面
     * @param ${key}Code  ${ZhKey}代码
     */
    @RequestMapping("/detail")
    public String toDetailPage(String ${key}Code,Model model){
        ${UpperKey} ${key} = ${key}Service.get${UpperKey}ByCode(${key}Code);
        model.addAttribute("${key}", ${key});

        model.addAttribute("nation", regionService.getRegionByCode(${key}.getNationCode()));
        return "mdm/${key}/detail";
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/${key}/list";
    }

    /**
     * 分页查询数据
     * @param ${key} ${ZhKey}对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<${UpperKey}> get${UpperKey}Data(${UpperKey} ${key} ,DataTableRequest dtrequest){
        int cnt = (int)${key}Service.get${UpperKey}Count(${key});
        List<${UpperKey}> list = ${key}Service.query${UpperKey}s(${key}, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<${UpperKey}> result = new PaginationResult<${UpperKey}>(list, pi);
        return new DataTableResponse<${UpperKey}>(dtrequest.getsEcho(),result);
    }

}
